<script lang="ts" setup>
import { reactive, ref, watch } from "vue"
import { createTableDataApi, deleteTableDataApi, updateTableDataApi, getTableDataApi } from "@/api/table"
import { getLogDetailApi } from "@/api/log"
import { type CreateOrUpdateTableRequestData, type GetTableData } from "@/api/table/types/table"
import { type GetTableResponseData } from "@/api/log/types/log"
import { type FormInstance, type FormRules, ElMessage, ElMessageBox } from "element-plus"
import { Search, Refresh, CirclePlus, Delete, Download, RefreshRight } from "@element-plus/icons-vue"
import { usePagination } from "@/hooks/usePagination"
import { type RouteRecordRaw, useRoute, useRouter } from "vue-router"
import VueJsonPretty from "vue-json-pretty"
import vkbeautify from "vkbeautify"

const router = useRouter()

defineOptions({
  // 命名当前组件
  name: "logDetail"
})

const loading = ref<boolean>(false)
const logDetail = ref<any>({})
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()

//#region 查

const searchFormRef = ref<FormInstance | null>(null)
const searchData = reactive({
  username: "",
  phone: ""
})

const getTableData = () => {
  const id = router.currentRoute.value.params.id
  loading.value = true
  getLogDetailApi({
    id
  })
    .then((res) => {
      console.log(res)

      logDetail.value = res.data
      if (res.data.dataStyle == 2) {
        logDetail.value.requestParameter = JSON.parse(res.data.requestParameter)
        logDetail.value.responseData = JSON.parse(res.data.responseData)
      } else {
        logDetail.value.requestParameter = vkbeautify.xml(res.data.requestParameter)
        logDetail.value.responseData = vkbeautify.xml(res.data.responseData)
        // console.log("logDetail.value.requestParameter", logDetail.value.requestParameter)
      }

      console.log("res.data", res.data)
    })
    .catch(() => {})
    .finally(() => {
      loading.value = false
    })
}
const handleSearch = () => {
  paginationData.startPage === 1 ? getTableData() : (paginationData.startPage = 1)
}
const resetSearch = () => {
  searchFormRef.value?.resetFields()
  handleSearch()
}
//#endregion

/** 监听分页参数的变化 */
watch([() => paginationData.startPage, () => paginationData.pageSize], getTableData, { immediate: true })
</script>

<template>
  <div class="app-container">
    <el-card v-loading="loading" shadow="never" class="search-wrapper">
      <!-- <el-form ref="searchFormRef" :inline="true" :model="searchData">
        <el-form-item prop="username" label="用户名">
          <el-input v-model="searchData.username" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="phone" label="手机号">
          <el-input v-model="searchData.phone" placeholder="请输入" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form> -->
    </el-card>
    <el-card v-loading="loading" shadow="never">
      <el-card style="">
        <template #header>
          <div class="card-header">
            <span>请求参数</span>
          </div>
        </template>
        <div>
          <div v-if="logDetail.dataStyle == 2" class="showData">
            <vue-json-pretty :data="logDetail.requestParameter" />
          </div>

          <div v-if="logDetail.dataStyle == 3">
            <highlightjs class="showData" language="xml" :autodetect="true" :code="logDetail.requestParameter" />
          </div>
        </div>
      </el-card>
      <br />

      <el-card style="">
        <template #header>
          <div class="card-header">
            <span>接口响应</span>
          </div>
        </template>
        <div>
          <div v-if="logDetail.dataStyle == 2" class="showData">
            <vue-json-pretty :data="logDetail.responseData" />
          </div>
          <div v-if="logDetail.dataStyle == 3 && logDetail.responseData != null">
            <highlightjs class="showData" language="JavaScript" :autodetect="true" :code="logDetail.responseData" />
          </div>
        </div>
      </el-card>
      <br />
      <el-card style="">
        <template #header>
          <div class="card-header">
            <span>原始响应</span>
          </div>
        </template>
        <div>
          <pre> {{ logDetail.rawResponse }}</pre>
        </div>
      </el-card>
    </el-card>

    <!-- <el-dialog
    v-model="dialogVisible"
    title="Tips"
    width="500"
    :before-close="handleClose"
  >
    <span>This is a message</span>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">Cancel</el-button>
        <el-button type="primary" @click="dialogVisible = false">
          Confirm
        </el-button>
      </div>
    </template>
  </el-dialog> -->
  </div>
</template>

<style lang="scss" scoped>
.search-wrapper {
  margin-bottom: 20px;
  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.table-wrapper {
  margin-bottom: 20px;
}

.pager-wrapper {
  display: flex;
  justify-content: flex-end;
}

.showData {
  // height: 550px;
  overflow-y: scroll;
}
</style>
